<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

/** @var yii\web\View $this */
/** @var app\models\Region $model */

$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Regions', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);

$js =<<< JS


class MapManager {
  constructor(mapId, long, lat, zoom) {
    this.map = L.map(mapId).setView([long, lat], zoom);
    this.initializeMap();
  }

  initializeMap() {
    L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
      attribution:
        '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(this.map);
  }

  addMarker(long, lat, popupText) {
    const marker = L.marker([long, lat]).addTo(this.map);
    if (popupText) {
      marker.bindPopup(popupText).openPopup();
    }
  }

  addCircle(long, lat, options, popupText) {
    const circle = L.circle([long, lat], options).addTo(this.map);
    if (popupText) {
      circle.bindPopup(popupText);
    }
  }

  addPolygon(latlngs, popupText) {
    const polygon = L.polygon(latlngs).addTo(this.map);
    if (popupText) {
      polygon.bindPopup(popupText);
    }
  }
}

// Инициализация карты
const mapManager = new MapManager("mapid", 51.505, -0.09, 13);

// Добавление маркера
mapManager.addMarker(51.505, -0.09, "<b>Привет!</b><br>Я маркер.");

// Добавление круга
mapManager.addCircle(
  51.508,
  -0.11,
  {
    color: "red",
    fillColor: "#f03",
    fillOpacity: 0.5,
    radius: 500
  },
  "Я круг."
);

// Добавление полигона
mapManager.addPolygon(
  [
    [51.509, -0.08],
    [51.503, -0.06],
    [51.51, -0.047]
  ],
  "Я полигон."
);

JS;
?>
<div class="region-view">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a('Update', ['update', 'regionId' => $model->id], ['class' => 'btn btn-primary']) ?>
        <?= Html::a('Delete', ['delete', 'regionId' => $model->id], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
        ]) ?>
    </p>

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            'regionType',
            'accountId',
            'parentRegion',
            'name',
            'latitude1',
            'latitude2',
            'longitude1',
            'longitude2',
        ],
    ]) ?>

<div id="mapid" style="width: 600px; height: 400px;"></div>


</div>
